package com.study_day01.controller;



import com.study_day01.entiry.Product;
import com.study_day01.entiry.ResponseData;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {
    private List<Product> products = new ArrayList<>();

    @GetMapping("/")
    public ResponseData<List<Product>> getAllProducts() {
        return new ResponseData<>("200", "Success", true, products);
    }

    @GetMapping("/{id}")
    public ResponseData<Product> getProductById(@PathVariable Long id) {
        Product product = products.stream().filter(p -> p.getId().equals(id)).findFirst().orElse(null);
        return product != null ?
                new ResponseData<>("200", "Success", true, product) :
                new ResponseData<>("404", "Product not found", false, null);
    }

    @PostMapping("/")
    public ResponseData<Product> addProduct(@RequestBody Product product) {
        products.add(product);
        return new ResponseData<>("201", "Product added successfully", true, product);
    }

    @PutMapping("/{id}")
    public ResponseData<Product> updateProduct(@PathVariable Long id, @RequestBody Product updatedProduct) {
        Product productToUpdate = products.stream().filter(p -> p.getId().equals(id)).findFirst().orElse(null);
        if (productToUpdate != null) {
            productToUpdate.setName(updatedProduct.getName());
            productToUpdate.setPrice(updatedProduct.getPrice());
            return new ResponseData<>("200", "Product updated successfully", true, productToUpdate);
        } else {
            return new ResponseData<>("404", "Product not found", false, null);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseData<Void> deleteProduct(@PathVariable Long id) {
        Product productToDelete = products.stream().filter(p -> p.getId().equals(id)).findFirst().orElse(null);
        if (productToDelete != null) {
            products.remove(productToDelete);
            return new ResponseData<>("204", "Product deleted successfully", true, null);
        } else {
            return new ResponseData<>("404", "Product not found", false, null);
        }
    }

    @PostMapping("/upload")
    public ResponseData<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
        String uploadPath = "uploads/" + LocalDate.now().toString(); // Dynamic folder creation
        File folder = new File(uploadPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        try {
            file.transferTo(new File(folder.getPath() + "/" + file.getOriginalFilename()));
            return new ResponseData<>("200", "File uploaded successfully", true, "File uploaded successfully");
        } catch (IOException e) {
            return new ResponseData<>("500", "Failed to upload file", false, "Failed to upload file");
        }
    }
}
